from numpy.lib import stride_tricks
import numpy as np


def test_as_strided():
    a = np.array([1, 2, 3, 4, 5])
    rows = 5
    cols = 2
    ele_stride = a.strides[-1]
    b = stride_tricks.as_strided(a, shape=(rows, cols), strides=(ele_stride, ele_stride)).copy()
    return b


def main():
    b = test_as_strided()
    print(b)


# # =============== Method 1 =================
#
# def vad1():
#     e1 = 18
#     e2 = 18
#     z1 = 20
#     wav_file = '/Users/d/Project/python_project/speech/wavs/connected.wav'
#     window_len = 0.02
#     hop_len = 0.05
#     zcr_threshold = 150
#
#     sr, signals = siowav.read(wav_file)
#     energy, t = st_energy(wav_file, window_len, hop_len, hamming_window)
#     zcr, t = st_zcr(wav_file, window_len, hop_len, zcr_threshold, hamming_window)
#
#     e_start_sign = np.sign(energy - e1)
#     e_starts = np.where((e_start_sign[1:] - e_start_sign[:-1]) > 0)[0]
#
#     e_end_sign = np.sign(energy - e2)
#     e_ends = np.where((e_end_sign[1:] - e_end_sign[:-1]) < 0)[0]
#
#     zcr_start_sign = np.sign(zcr - z1)
#     zcr_starts = np.where((zcr_start_sign[1:] - zcr_start_sign[:-1] > 0))[0]
#     point_list = list()
#     for zcr_start in zcr_starts:
#         point_list.append((zcr_start, 0))
#     for e_start in e_starts:
#         point_list.append((e_start, 1))
#     for e_end in e_ends:
#         point_list.append((e_end, 2))
#
#     point_list = sorted(point_list, key=lambda x: x[0])
#     start_list = list()
#     for i in range(len(point_list)):
#         if point_list[i][1] < 2:
#             if i == 0:
#                 start_list.append(point_list[i][0])
#             elif point_list[i-1][1] == 2:
#                 start_list.append(point_list[i][0])
#     start_times = t[np.array(start_list)]
#     end_times = t[e_ends]
#     draw1(sr, signals, t, energy, zcr, start_times, end_times)
#     plt.show()
#
#
# def draw1(sr, signal, t, energy, czr, start_times, end_times):
#     plt.subplot(3, 1, 1)
#     plt.plot(1/sr * np.arange(len(signal)), signal)
#     for start_time in start_times:
#         plt.axvline(start_time, color='g')
#     for end_time in end_times:
#         plt.axvline(end_time, color='r')
#     plt.title("波形", horizontalalignment='right')
#     plt.subplot(3, 1, 2)
#     plt.plot(t, energy, horizontalalignment='right')
#     plt.title("能量")
#     plt.subplot(3, 1, 3)
#     plt.plot(t, czr)
#     plt.title("过零率", horizontalalignment='right')
#
#
# # ================== Method 2 ========================
#
# def vad2():
#     e1 = 4
#     e2 = -3.5
#     z1 = 20
#     wav_file = '/Users/d/Project/python_project/speech/wavs/connected.wav'
#     window_len = 0.02
#     hop_len = 0.05
#     zcr_threshold = 150
#
#     sr, signals = siowav.read(wav_file)
#     energy, t = st_energy(wav_file, window_len, hop_len, hamming_window)
#     zcr, t = st_zcr(wav_file, window_len, hop_len, zcr_threshold, hamming_window)
#     energy_diff = energy[1:] - energy[:-1]
#     zcr_diff = zcr[1:] - zcr[:-1]
#
#     # e_start_sign = np.sign(energy_diff - e1)
#     e_starts = np.where((energy_diff - e1) > 0)[0]
#
#     # e_end_sign = np.sign(energy_diff - e2)
#     e_ends = np.where((energy_diff - e2) < 0)[0]
#
#     zcr_start_sign = np.sign(zcr_diff - z1)
#     zcr_starts = np.where((zcr_start_sign[1:] - zcr_start_sign[:-1] > 0))[0]
#
#     point_list = list()
#     for zcr_start in zcr_starts:
#         point_list.append((zcr_start, 0))
#     for e_start in e_starts:
#         point_list.append((e_start, 1))
#     for e_end in e_ends:
#         point_list.append((e_end, 2))
#
#     point_list = sorted(point_list, key=lambda x: x[0])
#     start_list = list()
#     for i in range(len(point_list)):
#         if point_list[i][1] < 2:
#             if i == 0:
#                 start_list.append(point_list[i][0])
#             elif point_list[i - 1][1] == 2:
#                 start_list.append(point_list[i][0])
#     start_times = t[np.array(start_list)]
#     end_times = t[e_ends]
#     draw2(sr, signals, t[:-1], energy_diff, zcr_diff, start_times, end_times)
#     plt.show()
#
#
# def draw2(sr, signal, t, energy_diff, czr_diff, start_times, end_times):
#     plt.subplot(3, 1, 1)
#     plt.plot(1/sr * np.arange(len(signal)), signal)
#     for start_time in start_times:
#         plt.axvline(start_time, color='g')
#     for end_time in end_times:
#         plt.axvline(end_time, color='r')
#     plt.ylabel("波形")
#     plt.subplot(3, 1, 2)
#     plt.plot(t, energy_diff)
#     plt.ylabel("能量差分")
#     plt.subplot(3, 1, 3)
#     plt.plot(t, czr_diff)
#     plt.ylabel("过零率差分")
#     plt.xlabel("时间")
#
#
# def main():
#     wav_file = '/Users/d/Project/python_project/speech/wavs/connected.wav'
#     window_len = 0.03
#     hop_len = 0.01
#     sr, signals = siowav.read(wav_file)
#     threshold = 150
#     energy, t = st_energy(wav_file, window_len, hop_len, hamming_window)
#     # zcr, t = st_zcr2(wav_file, window_len, hop_len, gaussian_window)
#     zcr, t = st_zcr(wav_file, window_len, hop_len, threshold, hamming_window)
#     plt.subplot(3, 1, 1)
#     plt.plot(1/sr * np.arange(len(signals)), signals)
#     plt.subplot(3, 1, 2)
#     plt.plot(t, energy)
#     plt.subplot(3, 1, 3)
#     plt.plot(t, zcr)
#     # plt.ylim([0, 50])
#     # plt.plot(t[:-1], log_energy[1:] - log_energy[:-1])
#     plt.show()

if __name__ == '__main__':
    main()